package net.sk.china.city.talk.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sk.china.city.talk.model.system.SystemNotification;
import net.sk.china.city.talk.request.notification.NotificationInfoRequest;
import net.sk.china.city.talk.request.notification.NotificationSearchRequest;
import net.sk.china.city.talk.request.notification.NotificationUpdateRequest;
import net.sk.china.city.talk.response.NotificationInfoResponse;
import net.sk.china.city.talk.service.system.SystemNotificationService;
import net.sk.china.common.response.CommonResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/6/2 9:36
 * // +----------------------------------------------------------------------
 */

@RestController
@RequestMapping("system/notification/")
@Api(tags = "通知设置")
public class SystemNotificationController {

    private final SystemNotificationService systemNotificationService;

    public SystemNotificationController(SystemNotificationService systemNotificationService) {
        this.systemNotificationService = systemNotificationService;
    }

    @PreAuthorize("hasAuthority('admin:system:notification:list')")
    @ApiOperation(value = "系统通知列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult<List<SystemNotification>> getList(@Validated NotificationSearchRequest request) {
        return CommonResult.success(systemNotificationService.getList(request));
    }

    @PreAuthorize("hasAuthority('admin:system:notification:wechat:switch')")
    @ApiOperation(value = "公众号模板开关")
    @RequestMapping(value = "wechat/switch/{id}", method = RequestMethod.POST)
    public CommonResult<Object> wechatSwitch(@PathVariable Integer id) {
        if (systemNotificationService.wechatSwitch(id)) {
            return CommonResult.success("更改成功");
        }
        return CommonResult.failed("更改失败");
    }

    @PreAuthorize("hasAuthority('admin:system:notification:routine:switch')")
    @ApiOperation(value = "小程序订阅模板开关")
    @RequestMapping(value = "routine/switch/{id}", method = RequestMethod.POST)
    public CommonResult<Object> routineSwitch(@PathVariable Integer id) {
        if (systemNotificationService.routineSwitch(id)) {
            return CommonResult.success("更改成功");
        }
        return CommonResult.failed("更改失败");
    }

    @PreAuthorize("hasAuthority('admin:system:notification:sms:switch')")
    @ApiOperation(value = "发送短信开关")
    @RequestMapping(value = "sms/switch/{id}", method = RequestMethod.POST)
    public CommonResult<Object> smsSwitch(@PathVariable Integer id) {
        if (systemNotificationService.smsSwitch(id)) {
            return CommonResult.success("更改成功");
        }
        return CommonResult.failed("更改失败");
    }

    @PreAuthorize("hasAuthority('admin:system:notification:detail')")
    @ApiOperation(value = "通知详情")
    @RequestMapping(value = "detail", method = RequestMethod.GET)
    public CommonResult<NotificationInfoResponse> info(@Validated NotificationInfoRequest request) {
        return CommonResult.success(systemNotificationService.getDetail(request));
    }

    @PreAuthorize("hasAuthority('admin:system:notification:update')")
    @ApiOperation(value = "修改通知")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public CommonResult<Object> update(@Validated @RequestBody NotificationUpdateRequest request) {
        if (systemNotificationService.modify(request)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}
